<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />

			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索名称" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table">
				<template #slot-btn="{ scope }">
					<el-button text bg @click="ViewPayCode(scope.row)">支付码</el-button>
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
	<cl-dialog title="支付码" v-model="visible" width="720">
		<div class="code-main">
			<p class="code-box">
				<QrcodeVue :value="payUrl" :size="240" level="H" />
			</p>
		</div>
	</cl-dialog>
</template>

<script lang="ts" name="flock-info" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { onMounted, reactive, ref } from "vue";

import QrcodeVue from "qrcode.vue";

const { service } = useCool();

// 选项
const optionsMembers: any = ref([
	{ label: "普通", value: 0, type: "danger" },
	{ label: "置顶", value: 1, type: "success" }
]);

const optionsComments: any = ref([
	{ label: "普通", value: 0, type: "danger" },
	{ label: "置顶", value: 1, type: "success" }
]);

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "支付名称",
			prop: "payName",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "名称",
			prop: "name",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "描述",
			prop: "description",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } }
		},
		{ label: "头像", prop: "avatar", component: { name: "cl-upload" } },
		{
			label: "群成员",
			prop: "members",
			component: {
				name: "el-select",
				options: optionsMembers,
				props: { clearable: true, multiple: true }
			}
		},
		{
			label: "群评论",
			prop: "comments",
			component: {
				name: "el-select",
				options: optionsComments,
				props: { clearable: true, multiple: true }
			}
		},
		{
			label: "群简介",
			prop: "summary",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } }
		},
		{
			label: "群成员数",
			prop: "flockNum",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "阅读量",
			prop: "views",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "收藏数",
			prop: "favorites",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "点赞数",
			prop: "likes",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "单价",
			prop: "price",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "次日价",
			prop: "nextDayPrice",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "qq群号",
			prop: "flockQrCodeNumber",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{ label: "群二维码", prop: "flockQrCode", component: { name: "cl-upload" } }
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "名称", prop: "name", minWidth: 140 },
		{ label: "描述", prop: "description", showOverflowTooltip: true, minWidth: 200 },
		{
			label: "头像",
			prop: "avatar",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{ label: "群简介", prop: "summary", showOverflowTooltip: true, minWidth: 200 },
		{ label: "阅读量", prop: "views", minWidth: 140 },
		{ label: "收藏数", prop: "favorites", minWidth: 140 },
		{ label: "点赞数", prop: "likes", minWidth: 140 },
		{ label: "单价", prop: "price", minWidth: 140 },
		{ label: "次日价", prop: "nextDayPrice", minWidth: 140 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete", "slot-btn"], width: 240 }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.flock.info
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

const GetMembersOptions = async () => {
	let data = await service.flock_user.user.list();
	optionsMembers.value = data.map((o: any) => {
		return {
			value: o.id + "",
			label: o.username
		};
	});

	let resData = await service.flock.comment.list();
	optionsComments.value = resData.map((o: any) => {
		return {
			value: o.id + "",
			label: o.content
		};
	});
	refresh();
};

let visible = ref(false);
let payUrl = ref("");
const ViewPayCode = (row) => {
	let _url = "/goodsFlockOrder?goodsId=";
	let url = "http://client.dearweb.cn" + _url + row.id;
	payUrl.value = url;
	visible.value = true;
};

onMounted(() => {
	GetMembersOptions();
});
</script>

<style scoped lang="scss">
.code-box {
	display: flex;
	justify-content: center;
	padding: 40px;
	position: relative;
	img {
		position: absolute;
		top: 50%;
		left: 50%;
		width: 60px;
		margin: -30px 0 0 -30px;
		border-radius: 8px;
	}
}
.code-main {
	display: flex;
	justify-content: center;
}
</style>
